package com.orkasgb.framework.redis.api;


import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;

public class JedisAPITest {

	/**
	 * 创建链接
	 */
	@Test
	public void testConnect() {
		Jedis jedis = new Jedis("172.25.184.9", 6380);
		String ping = jedis.ping();
		System.out.println(ping);
	}

	/**
	 * Strings
	 */
	@Test
	public void testStrings() {
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		// 设置一个key-value
		jedisCluster.set("name", "lucy");

		// 设置一个key-value，并设置该key的过期时间为10s。
		//jedisCluster.setex("name1", 10,"lucy");

		// 一次性设置多个key-value
		jedisCluster.mset("{user}:id", "001", "{user}:age", "20", "{user}:address", "beijing");

		// 获取值
		System.out.println(jedisCluster.get("name"));
		//System.out.println(jedisCluster.get("name1"));
		System.out.println(jedisCluster.mget("{user}:id", "{user}:age", "{user}:address"));

		// 查看过期时间
//		long ttl = 10L;
//		while(ttl != -2L) {
//			ttl = jedisCluster.ttl("name1");
//			System.out.println(ttl);
//		}
	}

	/**
	 * Lists
	 */
	@Test
	public void testLists() {
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		// 设置一个key-value
		jedisCluster.lpush("lists", "lucy", "kaka", "lala");

		// 获取值
		System.out.println(jedisCluster.lrange("lists", 0, -1));
		System.out.println(jedisCluster.lpop("lists"));
	}

	/**
	 * Lists
	 */
	@Test
	public void testSets() {
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		// 设置一个key-value
		jedisCluster.sadd("sets", "lucy", "kaka", "lucy");

		// 获取值
		System.out.println(jedisCluster.smembers("sets"));
	}

	/**
	 * Hashs
	 */
	@Test
	public void testHashs() {
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		// 设置一个key-value
		jedisCluster.hset("user", "name", "kaka");
		jedisCluster.hset("user", "age", "20");
		jedisCluster.hset("user", "phone", "110");

		HashMap<String, String> map = new HashMap<>();
		map.put("id", "01");
		map.put("pname", "pname");
		jedisCluster.hset("product", map);

		// 获取值
		System.out.println(jedisCluster.hget("user", "name"));
		System.out.println(jedisCluster.hget("product", "pname"));
	}

	/**
	 * Sorted sets
	 */
	@Test
	public void testSortedSets() {
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		// 设置一个key-value
		jedisCluster.zadd("city", 100D, "beijing");
		jedisCluster.zadd("city", 98D, "shanghai");
		jedisCluster.zadd("city", 95D, "guanghzou");

		// 获取值
		System.out.println(jedisCluster.zrange("city", 0, -1));
	}

	/**
	 * 手机验证码案例
	 */
	@Test
	public void testMobileVerificationCode() {
		// 1.获取6位手机验证码
		String randomCode = getRandomCode();

		// 2.将手机验证码存入redis，并设置过期时间为60s。
		JedisCluster jedisCluster = new JedisCluster(HostAndPort.from("172.25.184.9:6380"));
		jedisCluster.setex("code",60, randomCode);
		System.out.println("【"+randomCode+"】"+ "6位手机验证码已经发送到手机.");

		// 3.获取用户输入的验证码
		System.out.println("请输入6位手机验证码：");
		Scanner scanner = new Scanner(System.in);
		String next = String.valueOf(scanner.nextInt());

		// 4.和redis中的验证码比较，并反馈验证结果
		String code = jedisCluster.get("code");
		if (StringUtils.equals(code, next)) {
			System.out.println("验证成功");
		} else {
			System.out.println("验证失败");
		}


	}

	private String getRandomCode() {
		Random random = new Random();
		StringBuilder code = new StringBuilder("");
		for (int i = 0; i < 6; i++) {
			code.append(random.nextInt(10));
		}

		return code.toString();
	}
}
